/*
 *   Copyright (C) 2011 David Swords
 *	
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.net.*;
import java.util.Calendar;

public class ClientControl{
	
 	public static void main(String[] args) throws UnknownHostException, IOException {
 		String message;
 		Socket clientSocket = new Socket("localhost", 2000);
 		LANdroid land = new LANdroid(0, args[0]);
 		System.out.println("Connected...");
 		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 		
 	 		
 	 	while(true) {
 	 			message = inFromServer.readLine();
 	 			System.out.println("FROM SERVER: " + message);

				if(message.contains("L")) {
					System.out.println("Command Recognised: Left");
					Calendar cal = Calendar.getInstance();
					long s_time = cal.getTimeInMillis();
					long e_time = s_time + 100;
					
					while(s_time < e_time) {
						cal = Calendar.getInstance();
						s_time = cal.getTimeInMillis();
						land.left();
					}
				}
 						
				if(message.contains("R")) {
					System.out.println("Command Recognised: Right");
					Calendar cal = Calendar.getInstance();
					long s_time = cal.getTimeInMillis();
					long e_time = s_time + 100;
					
					while(s_time < e_time) {
						cal = Calendar.getInstance();
						s_time = cal.getTimeInMillis();
						land.right();
					}
				}
 						
				if(message.contains("B")) {
					System.out.println("Command Recognised: Backward");
					Calendar cal = Calendar.getInstance();
					long s_time = cal.getTimeInMillis();
					long e_time = s_time + 100;
					
					while(s_time < e_time) {
						cal = Calendar.getInstance();
						s_time = cal.getTimeInMillis();
						land.backward();
					}
				}

				if(message.contains("F")) {
					System.out.println("Command Recognised: Forward");
					Calendar cal = Calendar.getInstance();
					long s_time = cal.getTimeInMillis();
					long e_time = s_time + 100;
					
					while(s_time < e_time) {
						cal = Calendar.getInstance();
						s_time = cal.getTimeInMillis();
						land.forward();
					}
				}
 						
				if(message.contains("S")) {
					System.out.println("Command Recognised: Stop");
					Calendar cal = Calendar.getInstance();
					long s_time = cal.getTimeInMillis();
					long e_time = s_time + 100;
					
					while(s_time < e_time) {
						cal = Calendar.getInstance();
						s_time = cal.getTimeInMillis();
						land.stop();
				}
			}
		}
	}
}